﻿#region Using Directives

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

using QuickDev.Common.DB;
using QuickDev.Common.Utils;

using QuickDev.Model;

#endregion

namespace QuickDev.SQLServerDAL
{
    /// <summary>
    /// Data Access Layer For dbo.OPM_ReturnOrderHeader.
    /// </summary>
    public class ReturnOrderHeaderDAL
    {
        #region IsExist

        /// <summary>
        /// public static bool IsExist( string wangWangId , string customerCellphone )
        /// </summary>
        /// <param name="wangWangId">wangWangId</param>
        /// <param name="customerCellphone">customerCellphone</param>
        /// <returns>bool Existed</returns>
        public static bool IsExist( string wangWangId , string customerCellphone )
        {
            string procedureName = "OPM_ReturnOrderHeader_IsExist";

            SqlParameter[] parameters = {
					new SqlParameter("@WangWangId", SqlDbType.NVarChar,50),
                    new SqlParameter("@CustomerCellphone", SqlDbType.NVarChar,50),
                    new SqlParameter()
				};
            parameters[0].Value = wangWangId;
            parameters[1].Value = customerCellphone;
            parameters[2].Direction = ParameterDirection.ReturnValue;
            try
            {
                SqlDataCommon.ExecuteNonQuery( CommandType.StoredProcedure , procedureName , parameters );
                return ( (int)( parameters[2].Value ) == 0 ? false : true );
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database" , ex );
            }
        }

        #endregion

        #region GetModel

        /// <summary>
        /// public ReturnOrderHeader GetModel( int id )
        /// </summary>
        /// <param name="id">id</param>
        /// <returns>ReturnOrderHeader model</returns>
        public static ReturnOrderHeader GetModel( int id )
        {
            string procedureName = "OPM_ReturnOrderHeader_GetModel";

            ReturnOrderHeader model = new ReturnOrderHeader();

            SqlParameter parameter = new SqlParameter( "@Id" , id );

            try
            {
                ReaderFactory.ReaderToModel<ReturnOrderHeader>( SqlDataCommon.ExecuteReader( CommandType.StoredProcedure , procedureName , parameter ) , model );
                return model;
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database" , ex );
            }
        }
        #endregion

        #region GetList

        /// <summary>
        /// public List(ReturnOrderHeader) GetList( int returnOrderStatusId )
        /// </summary>
        /// <param name="returnOrderStatusId">returnOrderStatusId</param>
        /// <returns>List(ReturnOrderHeader)</returns>
        public static List<ReturnOrderHeader> GetList( int returnOrderStatusId )
        {
            string procedureName = "OPM_ReturnOrderHeader_GetList";

            List<ReturnOrderHeader> modelList = new List<ReturnOrderHeader>();

            SqlParameter parameter = new SqlParameter( "@ReturnOrderStatusId" , returnOrderStatusId );

            try
            {
                ReaderFactory.ReaderToList<ReturnOrderHeader>( SqlDataCommon.ExecuteReader( CommandType.StoredProcedure , procedureName , parameter ) , modelList );
                return modelList;
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database" , ex );
            }
        }
        #endregion

        #region Insert

        /// <summary>
        /// public static int Insert( string wangWangId, string customerName, string customerCellphone, string customerAddress , int createdBy )
        /// </summary>
        /// <param name="wangWangId">wangWangId</param>
        /// <param name="customerName">customerName</param>
        /// <param name="customerCellphone">customerCellphone</param>
        /// <param name="customerAddress">customerAddress</param>
        /// <param name="createBy">createBy</param>
        /// <returns>Id</returns>
        public static int Insert( string wangWangId , string customerName , string customerCellphone , string customerAddress , int createdBy )
        {
            if ( IsExist( wangWangId , customerCellphone ) )
                throw new ArgumentException
                    ( String.Format( "ReturnOrderHeader {0} - {1} is already existed." , wangWangId , customerCellphone ) );

            string procedureName = "OPM_ReturnOrderHeader_Insert";

            SqlParameter[] parameters = {
				new SqlParameter("@WangWangId", SqlDbType.NVarChar,50),	
				new SqlParameter("@CustomerName", SqlDbType.NVarChar,50),	
				new SqlParameter("@CustomerCellphone", SqlDbType.NVarChar,50),	
                new SqlParameter("@CustomerAddress", SqlDbType.NVarChar,500),
				new SqlParameter("@CreatedBy", SqlDbType.Int,4),
                new SqlParameter("@Id", SqlDbType.Int,4)
			};

            int nIndex = 0;
            parameters[nIndex++].Value = wangWangId;
            parameters[nIndex++].Value = customerName;
            parameters[nIndex++].Value = customerCellphone;
            parameters[nIndex++].Value = customerAddress;
            parameters[nIndex++].Value = createdBy;
            parameters[nIndex].Direction = ParameterDirection.Output;

            try
            {
                SqlDataCommon.ExecuteNonQuery( CommandType.StoredProcedure , procedureName , parameters );

                return (int)Utility.ChangeTypeTo( parameters[nIndex].Value , typeof( Int32 ) );
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database" , ex );
            }
        }
        #endregion

        #region Update

        /// <summary>
        /// public static bool Update( int id, string wangWangId, string customerName, string customerCellphone, string customerAddress , int updatedBy )
        /// </summary>
        /// <param name="id">id</param>
        /// <param name="wangWangId">wangWangId</param>
        /// <param name="customerName">customerName</param>
        /// <param name="customerCellphone">customerCellphone</param>
        /// <param name="updatedBy">updatedBy</param>
        /// <returns>bool successful</returns>
        public static bool Update( int id , string wangWangId , string customerName , string customerCellphone , string customerAddress , int updatedBy )
        {
            string procedureName = "OPM_ReturnOrderHeader_Update";

            SqlParameter[] parameters = {
				new SqlParameter("@Id", SqlDbType.Int,4),	
				new SqlParameter("@WangWangId", SqlDbType.NVarChar,50),	
				new SqlParameter("@CustomerName", SqlDbType.NVarChar,50),	
				new SqlParameter("@CustomerCellphone", SqlDbType.NVarChar,50),	
                new SqlParameter("@CustomerAddress", SqlDbType.NVarChar,500),
				new SqlParameter("@UpdatedBy", SqlDbType.Int,4),
                new SqlParameter()
			};

            int nIndex = 0;
            parameters[nIndex++].Value = id;
            parameters[nIndex++].Value = wangWangId;
            parameters[nIndex++].Value = customerName;
            parameters[nIndex++].Value = customerCellphone;
            parameters[nIndex++].Value = customerAddress;
            parameters[nIndex++].Value = updatedBy;
            parameters[nIndex].Direction = ParameterDirection.ReturnValue;

            try
            {
                SqlDataCommon.ExecuteNonQuery( CommandType.StoredProcedure , procedureName , parameters );

                return ( (int)( parameters[nIndex].Value ) == 0 ? true : false );
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database" , ex );
            }
        }
        #endregion

        #region UpdateStatus

        /// <summary>
        /// public bool UpdateStatus( int id, int deletedBy )
        /// </summary>
        /// <param name="id">id</param>
        /// <param name="returnOrderStatusId">returnOrderStatusId </param>
        /// <param name="updatedBy">updatedBy </param>
        /// <returns>bool successful</returns>
        public static bool UpdateStatus( int id , int returnOrderStatusId , int updatedBy )
        {
            string procedureName = "OPM_ReturnOrderHeader_UpdateStatus";

            SqlParameter[] parameters = {
                new SqlParameter("@Id", SqlDbType.Int, 4),
                new SqlParameter("@ReturnOrderStatusId", SqlDbType.Int, 4),
                new SqlParameter("@UpdatedBy", SqlDbType.Int, 4),
                new SqlParameter()
			};

            parameters[0].Value = id;
            parameters[1].Value = returnOrderStatusId;
            parameters[2].Value = updatedBy;
            parameters[3].Direction = ParameterDirection.ReturnValue;
            try
            {
                SqlDataCommon.ExecuteNonQuery( CommandType.StoredProcedure , procedureName , parameters );

                return ( (int)( parameters[3].Value ) == 0 ? true : false );
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database" , ex );
            }
        }
        #endregion
    }
}

